package com.app.brilliant.game.service.impl;
import com.app.brilliant.common.service.impl.BaseServiceImpl;
import com.app.brilliant.game.bean.BriClassHour;
import com.app.brilliant.game.response.BriClassHourWithNextInfo;
import com.app.brilliant.game.response.BriClassHourWithType;
import com.app.brilliant.game.response.BriClassHourWithTypeInfo;
import com.app.brilliant.game.response.BriCourseCompleteInfo;
import com.app.brilliant.game.service.BriClassHourService;
import com.app.brilliant.game.mapper.BriClassHourMapper;
import com.app.brilliant.game.service.BriUserHourCompleteService;
import com.app.brilliant.game.service.WxConfigService;
import com.app.brilliant.sign.constant.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
/**
* @author GF
* @description 针对表【bri_class_hour】的数据库操作Service实现
* @createDate 2025-09-01 20:40:39
*/
@Service
public class BriClassHourServiceImpl extends BaseServiceImpl<BriClassHourMapper, BriClassHour>
implements BriClassHourService{
    @Autowired
    private BriUserHourCompleteService briUserHourCompleteService;
    @Autowired
    private WxConfigService wxConfigService;
    @Override
    public List<BriClassHourWithTypeInfo> getClassHourDetailsByCourseId(Long courseId,Integer uid) {
        List<BriClassHourWithType> list = baseMapper.getClassHourDetailsByCourseId(courseId.intValue());
        List<BriCourseCompleteInfo> courseCompleteInfo = briUserHourCompleteService.getCourseCompleteInfo(courseId.intValue(), uid);
        Map<Integer, List<BriClassHourWithType>> collect = list.stream().collect(Collectors.groupingBy(BriClassHourWithType::getTId));
        List<BriClassHourWithTypeInfo> briClassificationCourseInfos = new ArrayList<>();
        String domain = wxConfigService.getConfigValue(Constants.NomalConfigKey.PictureDomain);
        collect.forEach((key,value)->{
            if(!value.isEmpty()){
                BriClassHourWithType first = value.get(0);
                BriClassHourWithTypeInfo briClassHourWithTypeInfo = new BriClassHourWithTypeInfo();
                briClassHourWithTypeInfo.setName(first.getTypeName());
                briClassHourWithTypeInfo.setDesc(first.getDesc());
                List<BriClassHourWithTypeInfo.BriClassHourInfo> classHours = value.stream().map(v -> {
                    BriClassHourWithTypeInfo.BriClassHourInfo briClassHour = new BriClassHourWithTypeInfo.BriClassHourInfo();
                    briClassHour.setCId(v.getCId());
                    briClassHour.setExp(v.getExp());
                    briClassHour.setName(v.getName());
                    briClassHour.setId(v.getId());
                    briClassHour.setUrl(domain + v.getUrl());
                    boolean completed = false;
                    Optional<BriCourseCompleteInfo> completeInfo = courseCompleteInfo.stream().filter(q -> q.getClassHourId().compareTo(v.getId()) == 0).findFirst();
                    if(completeInfo.isPresent()){
                        completed = completeInfo.get().getIsComplete() == 1;
                    }
                    briClassHour.setCompleted(completed);
                    return briClassHour;
                }).collect(Collectors.toList());
                briClassHourWithTypeInfo.setList(classHours);
                briClassificationCourseInfos.add(briClassHourWithTypeInfo);
            }
        });
        List<BriClassHourWithTypeInfo.BriClassHourInfo> collected = briClassificationCourseInfos.stream().map(v -> v.getList()).flatMap(List::stream).collect(Collectors.toList());
        Optional<BriClassHourWithTypeInfo.BriClassHourInfo> completeInfo = collected.stream().filter(q -> !q.isCompleted()).findFirst();
        completeInfo.ifPresent(briClassHourInfo -> briClassHourInfo.setCurrent(true));
        return briClassificationCourseInfos;
    }

   @Override
    public List<BriClassHourWithNextInfo> getTopCompletionClassHours(Integer uid, Integer limit) {
        // 查询用户所有课程中完成度最高的课时数据
        return baseMapper.getTopCompletionClassHours(uid, limit);
    }

    @Override
    public BriClassHour getUnCompleteNextClassHour(Integer id) {
        return baseMapper.getUnCompleteNextClassHour(id);
    }
}
